package com.snts.synchronization.core.spring;

import java.sql.Connection;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.FactoryBean;
import com.snts.synchronization.core.Synchronization;
import com.snts.synchronization.core.XMLLoader;

public class SynchronizationFactory implements FactoryBean {

	private static final Log log = LogFactory.getLog(SynchronizationFactory.class);
	
	private DataSource dataSource;
	
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public Object getObject() throws Exception {
		System.out.println("Creating new instance of Synchronization");
		Synchronization synchronization = new Synchronization();
		new XMLLoader(synchronization).load();
		Connection conn = dataSource.getConnection();
		try {
			synchronization.load(conn.getMetaData());
			return synchronization;
		} finally {
			conn.close();
		}
	}

	@Override
	public Class getObjectType() {
		return Synchronization.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
